package com.qf.dao.impl;

import com.qf.dao.AdminDao;
import com.qf.pojo.Admin;
import com.qf.pojo.User;
import com.qf.utils.DruidJdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 高圆圆
 * @date 2022/12/26 11:30
 * 管理员数据访问接口实现
 */
public class AdminDaoImpl  implements AdminDao {
    /**
     * 通过用户名查询管理员
     * @param username 管理员名称
     * @return 返回管理员实体
     */
    @Override
    public Admin selectAdminByUsername(String username) throws SQLException {
        //创建执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from admin where username = ?" ;
        //执行
        //将查询的某一条结果封装到实体中
        Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), username);
        return  admin;
    }

    /**
     * 数据访问接口查询所有的用户
     * @return 返回用户列表
     */
    @Override
    public List<User> selectUsers() throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user" ;
        //将查询的多条记录封装到List集合中
        List<User> users = qr.query(sql, new BeanListHandler<User>(User.class));
        return users;
    }

    /**
     * 数据访问接口通过用户id查询指定的用户
     * @param uid 用户的uid
     * @return 返回用户实体
     */
    @Override
    public User selectUserByUid(String uid) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where uid = ?" ;
        //执行
        //将查询的某条记录封装到一个实体中
        User user = qr.query(sql, new BeanHandler<User>(User.class), uid);
        return user;
    }

    /**
     * 数据访问接口更新用户
     * @param user 用户实体
     * @return 返回的影响的行数
     */
    @Override
    public int updateUser(User user) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql  = "update user set username=?,password=?,name=?,email=?,sex=? where uid = ?" ;
        int count = qr.update(sql,
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getSex(),
                user.getUid());
        return count;
    }

    /**
     * 数据访问接口删除指定的用户
     * @param uid 用户编号
     * @return 影响的行数
     */
    @Override
    public int deleteUser(String uid) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "delete from user where uid = ?" ;
        int count = qr.update(sql, uid);
        return count;
    }

    /**
     * 用户的数据访问接口分页查询列表数据
     * @param currentPage  当前页码
     * @param pageSize     每页显示的条数
     * @return   返回的用户列表数据
     */
    @Override
    public List<User> selectUserByPage(int currentPage, int pageSize) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user limit ?,?" ;
        //查询
        //将查询的多条记录封装到list集合中
        List<User> users = qr.query(sql, new BeanListHandler<User>(User.class),
                (currentPage - 1) * pageSize,
                pageSize);
        return users;
    }

    /**
     * 查询用户的总记录数
     * @return 返回的总条数
     */
    @Override
    public int selectTotalCount() throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select count(uid) from user" ;
        //查询:将查询的单行单例数据封装Object对象中
        Object obj = qr.query(sql, new ScalarHandler<>());
        int totalCount = Integer.parseInt(String.valueOf(obj)) ;
        return totalCount;
    }
}
